package leetcode;

public class Lc14 {

    public String longestCommonPrefix(String[] strs) {
        if (strs[0].length() == 0) return "";
        char[] chars = strs[0].toCharArray();
        int end = chars.length - 1;

        for (int i = 1; i < strs.length; i++) {
            if ("".equals(strs[i])) return "";
            int j = 0;
            for (; j < strs[i].length() && j <=end ; j++) {
                if (chars[j] != strs[i].charAt(j)) {
                    break;
                }
            }
            end = j - 1;
            if (end == -1) return "";
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i <= end; i++) {
            builder.append(chars[i]);
        }
        return builder.toString();
    }


}
